package com.web3.management.controller;

import com.web3.management.dto.ApiResponse;
import com.web3.management.entity.SystemSetting;
import com.web3.management.service.SystemSettingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 系统配置控制器
 *
 * @author Web3 Management Team
 */
@Slf4j
@RestController
@RequestMapping("/api/system/settings")
@RequiredArgsConstructor
@Validated
public class SystemSettingController {

    private final SystemSettingService systemSettingService;

    /**
     * 获取所有系统配置
     *
     * @return 配置列表
     */
    @GetMapping
    public ApiResponse<List<SystemSetting>> getAllSettings() {
        List<SystemSetting> settings = systemSettingService.getAllSettings();
        return ApiResponse.success(settings);
    }

    /**
     * 获取指定配置
     *
     * @param settingKey 配置键
     * @return 配置信息
     */
    @GetMapping("/{settingKey}")
    public ApiResponse<SystemSetting> getSetting(@PathVariable @NotBlank String settingKey) {
        return systemSettingService.getSettingByKey(settingKey)
                .map(ApiResponse::success)
                .orElse(ApiResponse.error("配置不存在: " + settingKey));
    }

    /**
     * 获取随机休眠配置
     *
     * @return 随机休眠配置
     */
    @GetMapping("/random-sleep")
    public ApiResponse<Map<String, Object>> getRandomSleepConfig() {
        Map<String, Object> config = systemSettingService.getRandomSleepConfig();
        return ApiResponse.success(config);
    }

    /**
     * 获取监控配置
     *
     * @return 监控配置
     */
    @GetMapping("/monitor")
    public ApiResponse<Map<String, Object>> getMonitorConfig() {
        Map<String, Object> config = systemSettingService.getMonitorConfig();
        return ApiResponse.success(config);
    }

    /**
     * 获取通知配置
     *
     * @return 通知配置
     */
    @GetMapping("/notification")
    public ApiResponse<Map<String, Object>> getNotificationConfig() {
        Map<String, Object> config = systemSettingService.getNotificationConfig();
        return ApiResponse.success(config);
    }

    /**
     * 更新配置
     *
     * @param settingKey 配置键
     * @param request    配置请求
     * @return 更新后的配置
     */
    @PutMapping("/{settingKey}")
    public ApiResponse<SystemSetting> updateSetting(
            @PathVariable @NotBlank String settingKey,
            @RequestBody @Valid UpdateSettingRequest request) {

        SystemSetting setting = systemSettingService.updateSetting(
                settingKey,
                request.getSettingValue(),
                request.getDescription()
        );

        return ApiResponse.success(setting);
    }

    /**
     * 删除配置
     *
     * @param settingKey 配置键
     * @return 成功响应
     */
    @DeleteMapping("/{settingKey}")
    public ApiResponse<Void> deleteSetting(@PathVariable @NotBlank String settingKey) {
        systemSettingService.deleteSetting(settingKey);
        return ApiResponse.success();
    }

    /**
     * 初始化默认配置
     *
     * @return 成功响应
     */
    @PostMapping("/initialize")
    public ApiResponse<String> initializeDefaultSettings() {
        systemSettingService.initializeDefaultSettings();
        return ApiResponse.success("默认配置初始化完成");
    }

    /**
     * 配置更新请求DTO
     */
    public static class UpdateSettingRequest {
        private Object settingValue;
        private String description;

        public Object getSettingValue() {
            return settingValue;
        }

        public void setSettingValue(Object settingValue) {
            this.settingValue = settingValue;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }
}